﻿@inject IStringLocalizer<ValidateForms> Localizer
@inject IStringLocalizer<Foo> LocalizerFoo

<ValidateForm Model="@Model" OnValidSubmit="@OnValidSubmit" OnInvalidSubmit="@OnInvalidSubmit">
    <div class="row g-3">
        <div class="col-12 col-sm-6">
            <BootstrapInputGroupLabel @bind-Value="@Model.Name" />
            <BootstrapInputGroup>
                <Display @bind-Value="@Model.Name"></Display>
                <BootstrapInputGroupLabel DisplayText="Test" />
            </BootstrapInputGroup>
        </div>
        <div class="col-12 col-sm-6">
            <BootstrapInputNumber @bind-Value="@Model.Count" />
        </div>
        <div class="col-12 col-sm-6">
            <DateTimePicker @bind-Value="@Model.DateTime" />
        </div>
        <div class="col-12 col-sm-6">
            <Select @bind-Value="@Model.Education" />
        </div>
        <div class="col-12">
            <CheckboxList @bind-Value="@Model.Hobby" Items="@Hobbys" />
        </div>
        <div class="col-12">
            <Button ButtonType="@ButtonType.Submit" Icon="fa-fw fa-solid fa-floppy-disk" IsAsync="true" Text="@Localizer["ValidateFormsSubmitButtonText"]" />
        </div>
    </div>
</ValidateForm>
<ConsoleLogger @ref="Logger" class="mt-3" />

@code {
    [NotNull]
    private Foo? Model { get; set; }

    [NotNull]
    private IEnumerable<SelectedItem>? Hobbys { get; set; }

    [NotNull]
    private ConsoleLogger? Logger { get; set; }

    /// <summary>
    /// OnInitializedAsync 方法
    /// </summary>
    /// <returns></returns>
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        // 切换线程 模拟异步通过 webapi 加载数据
        await Task.Yield();

        Model = new() { Name = "Name", Education = EnumEducation.Primary, DateTime = DateTime.Now };

        // 初始化参数
        Hobbys = Foo.GenerateHobbys(LocalizerFoo);
    }

    private async Task OnValidSubmit(EditContext context)
    {
        Logger.Log(Localizer["OnValidSubmitSatringLog"]);
        await Task.Delay(3000);
        Logger.Log(Localizer["OnValidSubmitDoneLog"]);
    }

    private Task OnInvalidSubmit(EditContext context)
    {
        Logger.Log(Localizer["OnInvalidSubmitCallBackLog"]);
        return Task.CompletedTask;
    }
}
